﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using BlobTowerDefense.Managers;
using BlobTowerDefense.Tools;

namespace BlobTowerDefense.Game_Entities
{
    /// <summary>
    /// Trida uchovavajici dulezita data o strelach z vezi
    /// </summary>
    public class Bullet
    {
        public int Damage;
        private float speed;
        public Vector3 Position;
        private Curve3D bulletPath;
        public TOWERSPECIAL BulletSpecial;
        public Model Model;
        public bool Exists;
        private float bulletTime;
        public float Scale;

        private const float addTime = 0.001f;

        /// <summary>
        /// Konstruktor tridy Bullet
        /// </summary>
        public Bullet(Vector3 positionStart, Vector3 positionEnemy, BulletStats bulletStats)
        {
            Damage = bulletStats.towerStats.Damage;
            speed = bulletStats.BulletSpeed;
            Position = positionStart;
            bulletPath = new Curve3D(positionStart, positionEnemy, 0.0f, (1.0f/speed));
            BulletSpecial = bulletStats.towerStats.Special;
            Model = GamePage.content.Load<Model>(bulletStats.PathToModel);
            Exists = true;
            bulletTime = 0.0f;
            Scale = 0.3f;
        }

        public void Update()
        {
            bulletTime += addTime;
            if (bulletTime >= bulletPath.GetEndTime())
            {
                this.Exists = false;
            }

            this.Position = bulletPath.GetPosition(bulletTime);
        }



        public void Draw()
        {
            foreach (ModelMesh mesh in this.Model.Meshes)
            {
                foreach (BasicEffect currentEffect in mesh.Effects)
                {
                    currentEffect.Projection = Camera.ProjectionMatrix;
                    currentEffect.View = Camera.ViewMatrix;
                    currentEffect.World = Microsoft.Xna.Framework.Matrix.CreateScale(this.Scale) *
                                          Microsoft.Xna.Framework.Matrix.CreateTranslation(this.Position);
                    currentEffect.EnableDefaultLighting();

                    foreach (EffectPass pass in currentEffect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                        mesh.Draw();
                    }
                }
            }
        }

        public float normalizedTime { get; set; }
    }
}
